$(function () {
    zkdmxGrid($.webPath() + '/kcgl/zksq/zkd_zkdmx?zkdId=' + $("[name='zkd.id']").val() || '');

    $.post($.webPath() + '/organ/wls/wlsInuseList', function (jgdata) {
        $.post($.webPath() + '/cwgl/kmys/cyfyys/fylbcombobox', {'gsId':$("#zkd_sqgs_id").val()}, function (data) {
            zkdfyGrid($.webPath() + '/kcgl/zksq/zkdgl_zkfy?zkdId=' + $("[name='zkd.id']").val() || '', jgdata, data);
        })
    })
    //新增合同费用
    $("#zkd_zkdfy_toolbar [name='xzzkdfy']").click(function () {
        var data = {
            'sydw.id':null,
            'fylb.id':null,
            'jfsl':0,
            'dj':0,
            'je':0,
            'bz':null
        };
        $("#zkd_fy").edatagrid('appendRow', data);
    });
    //删除合同费用
    $("#zkd_zkdfy_toolbar [name='sczkdfy']").click(function () {
        var rows = $("#zkd_fy").edatagrid('getChecked');
        if (rows) {
            $.messager.confirm('提示', '是否确定删除转库费用?', function (r) {
                if (r) {
                    $.each(rows, function (i, val) {
                        var index = $("#zkd_fy").edatagrid('getRowIndex', val);
                        $("#zkd_fy").edatagrid('deleteRow', index);
                    });
                }
            });
        }
        $("#zkd_fy").edatagrid('resize');
    });

})

function zkdmxGrid(href, cdOptions, fsOptions) {
    $("#zkd_mx_action_table").edatagrid({
        url:href,
        fitColumns:false,
        singleSelect:true,
        fit:true,
        checkOnSelect:false,
        selectOnCheck:false,
        showFooter:true,
        rownumbers:true,
        toolbar:"#zkd_zkdmx_toolbar",
        columns:[
            [
                {field:'checked', checkbox:true},
                {field:'wzgg.id', title:'物资ID', hidden:true},
                {field:'wzgg.code', title:'物资编码', width:100, align:'center'},
                {field:'wzgg.name', title:'物资名称', width:100, align:'center'},
                {field:'wzgg.ggxh', title:'规格型号', width:100, align:'center'},
                {field:'kh.id', title:'卡号ID', hidden:true},
                {field:'kh.khbm', title:'卡号', width:120, align:'center'},
                {field:'zcsl', title:'转出数量', width:100, align:'right'},
                {field:'zrjs', editor:{type:'numberbox', options:{min:0, precision:0}}, title:'转入件数', width:100, align:'right'},
                {field:'zrsl', editor:{type:'numberbox', options:{min:0, precision:4}}, title:'转入数量', width:100, align:'right',
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'bz', editor:{type:'text'}, title:'备注', width:100, align:'center'}
            ]
        ],
        loadFilter:function (data) {
            $.each(data.rows, function () {
                if (this.zcsl) {
                    this.zrsl = this.zcsl;
                }
            })
            return data;
        },
        onSelect:function (rowIndex, rowData) {
            //获取每个单元格
            $("#zkd_mx_action_table").edatagrid('editRow', rowIndex);
            var editors = $("#zkd_mx_action_table").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
            var zcjsEditor = editors[0];
            var zcslEditor = editors[1];
            zcjsEditor.target.unbind('blur').bind('blur', function () {
                $(zcjsEditor.target).numberbox('setValue', $(zcjsEditor.target).val());
            });
            zcslEditor.target.unbind('blur').bind('blur', function () {
                $(zcslEditor.target).numberbox('setValue', $(zcslEditor.target).val());
            });
        }
    })
}

function zkdfyGrid(href, jgOptions, fsOptions) {
    $("#zkd_fy").edatagrid({
        url:href,
        fitColumns:true,
        singleSelect:true,
        fit:true,
        checkbox:true,
        checkOnSelect:false,
        selectOnCheck:false,
        rownumbers:true,
        toolbar:"#zkd_zkdfy_toolbar",
        columns:[
            [
                {field:'checked', checkbox:true},
                {field:'sydw.id', align:'center', editor:{type:'combobox', options:{
                    data:jgOptions,
                    valueField:'id',
                    textField:'qc',
                    multiple:false,
                    panelHeight:'200'
                }
                }, formatter:function (value) {
                    for (var i = 0; i < jgOptions.length; i++) {
                        if (jgOptions[i].id == value) return jgOptions[i].qc;
                    }
                    return value;
                }, title:'收益单位', width:50, align:'center'},

                {field:'fylb.id', align:'center', editor:{type:'combobox', options:{
                    data:fsOptions,
                    valueField:'id',
                    textField:'fykmjc',
                    multiple:false,
                    panelHeight:'110'
                }
                }, formatter:function (value) {
                    for (var i = 0; i < fsOptions.length; i++) {
                        if (fsOptions[i].id == value) return fsOptions[i].fykmjc;
                    }
                    return value;
                }, title:'费用类别', width:30},
                {field:'jfsl', editor:{type:'numberbox', options:{min:0, groupSeparator:',', precision:4}}, title:'计费数量',
                    formatter:function (value) {
                        var sl = formatSl(value, ",");
                        return sl;
                    }, width:40, align:'right'},
                {field:'dj', editor:{type:'numberbox', options:{min:0, groupSeparator:',', precision:4}}, title:'单价(元)',
                    formatter:function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width:40, align:'right'},
                {field:'je', editor:{type:'numberbox', options:{min:0, groupSeparator:',', precision:4}}, title:'金额(元)',
                    formatter:function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width:50, align:'right'},
                {field:'bz', editor:{type:'text'}, title:'备注', width:50, align:'center'}
            ]
        ],
        onSelect:function (rowIndex, rowData) {
            //获取每个单元格
            $("#zkd_fy").edatagrid('editRow', rowIndex);
            var editors = $("#zkd_fy").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
            var jfslEditor = editors[2];
            var djEditor = editors[3];
            var jeEditor = editors[4];
            jfslEditor.target.unbind("blur").bind('blur', function () {
                $(jfslEditor.target).numberbox('setValue', $(jfslEditor.target).val());
                $(jeEditor.target).numberbox('setValue', $(jfslEditor.target).numberbox('getValue') * $(djEditor.target).numberbox('getValue'));
            });
            djEditor.target.unbind("blur").bind('blur', function () {
                $(djEditor.target).numberbox('setValue', $(djEditor.target).val());
                $(jeEditor.target).numberbox('setValue', $(jfslEditor.target).numberbox('getValue') * $(djEditor.target).numberbox('getValue'));
            });

        }
    })
}




